package com.kanasco.vision.controller;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;
import org.springframework.web.servlet.view.RedirectView;

import bb.access.po.CustomerVendorAddressInfo;
import bb.access.po.SalesOrder;
import bb.common.SO2POAgent;

import com.kanasco.vision.model.Sales;
import com.kanasco.vision.service.ISales;

public class UpdateSO extends SimpleFormController {

    ISales salesServices;
	public ISales getSalesServices() {
		return salesServices;
	}
	public void setSalesServices(ISales salesServices) {
		this.salesServices = salesServices;
	}
	public UpdateSO() 
	{
		/*setCommandClass(Sales.class);
		setCommandName("sales");*/
		setCommandClass(SalesOrder.class);
		setCommandName("sales");
	}
	protected ModelAndView onSubmit(HttpServletRequest req,HttpServletResponse res,Object command,BindException exception) throws SQLException
  	{
		//Sales sales=(Sales) command;
		SalesOrder sales=(SalesOrder) command;
		String sno=sales.getSalesOrderNumber();
		System.out.println("The Sales Order Number is"+sno);
		
		SalesOrder so=new SalesOrder();
		String num=so.getSalesOrderNumber();
		System.out.println("The Sales Order Number is"+num);
		System.out.println("Now iam in onsubmit");
		String snum=req.getParameter("salesOrderNumber");
		String action=req.getParameter("action");
		System.out.println("Action from update is"+action);
		String cuno=sales.getCustomerId();
		System.out.println("Customer number"+cuno);
		CustomerVendorAddressInfo custinfo=salesServices.load(cuno);
		if(action.equalsIgnoreCase("update"))
		{
			System.out.println("iam in if codition ");
			so.setSalesOrderNumber(sales.getSalesOrderNumber());
			so.setRequester(sales.getSalesOrderNumber());
			so.setRequester(sales.getRequester());
			String reuester=sales.getRequester();
			System.out.println("Requester name is"+reuester);
			so.setCustomerPo(sales.getCustomerPo());
			so.setRelatedDocument(sales.getRelatedDocument());
			so.setReferenceSalesOrder(sales.getReferenceSalesOrder());
			so.setContact(sales.getContact());
			so.setCarrierAccount(sales.getCarrierAccount());
			so.setExchange(sales.getExchange());
			so.setCreditAccount(sales.getCreditAccount());
			so.setSalesOrderBoilerplate(sales.getSalesOrderBoilerplate());
			so.setSalesOrderComments(sales.getSalesOrderComments());
			so.setSalesOrderPrivateNotes(sales.getSalesOrderPrivateNotes());
			so.setLastUser("ADMIN");
			
			///so.setCustomerAddressInfo(sales.getCustomerAddressInfo());
			salesServices.update(so);
		}
		if(action.equalsIgnoreCase("view"))
		{
			CustomerVendorAddressInfo info=new CustomerVendorAddressInfo();
			String custId=sales.getCustomerId();
			System.out.println("Customer id is in view condition"+custId);
			info= salesServices.load(custId);
			return new ModelAndView("CustDetails","info",info);
	
		}
			ModelAndView mv=new ModelAndView("ViewSO","sales",sales);
			mv.addObject("custinfo",custinfo);
			return mv;
	/*	return new ModelAndView(new RedirectView("moveItem.htm"));*/
	
		}
	}
